package edu.tec.mty.extras.fuzzy;
import java.util.Vector;


public class FuzzyModel {
	Vector<FuzzyRule> Rules = new Vector<FuzzyRule>();
	double numerator1 = 0;
	double denominator1 = 0;
	double numerator2 = 0;
	double denominator2 = 0;
	double numerator3 = 0;
	double denominator3 = 0;

	double prom1 = 0;
	double prom2 = 0;
	double prom3 = 0;
	int noOfRules = 0;
	
	//Fuzzy Model Constructor
	public FuzzyModel(Vector<FuzzyRule> myRules, double minLimit1, double maxLimit1, 
					  double minLimit2, double maxLimit2, double minLimit3, double maxLimit3) {
		
		Rules = myRules;
		noOfRules = Rules.size();
		
		prom1 = (maxLimit1 + minLimit1) / 2;
		prom2 = (maxLimit2 + minLimit2) / 2;
		prom3 = (maxLimit3 + minLimit3) / 2;
		
	}
	
	//Gets the Output of the fuzzy model for the inputs
	public double getOut1(double Input1, double Input2, double Input3, double Input4, double Input5){
		double h1, y1;
		for(int i = 0; i < noOfRules; i++){
			h1 = Rules.get(i).ruleHeight(Input1, Input2, Input3, Input4, Input5);
			if (h1 > 0){
				y1 = Rules.get(i).getCenter1();
				numerator1 = numerator1 + (h1 * y1);
				denominator1 = denominator1 + h1;
			}
		}
		
		if(denominator1 == 0){
			denominator1 = 1;
			numerator1 = prom1;
		}
		
		return numerator1 / denominator1;
	}
	
	public double getOut2(double Input1, double Input2, double Input3, double Input4, double Input5){
		double h2, y2;
		for(int i = 0; i < noOfRules; i++){
			h2 = Rules.get(i).ruleHeight(Input1, Input2, Input3, Input4, Input5);
			if (h2 > 0){
				y2 = Rules.get(i).getCenter2();
				numerator2 = numerator2 + (h2 * y2);
				denominator2 = denominator2 + h2;
			}
		}
		
		if(denominator2 == 0){
			denominator2 = 1;
			numerator2 = prom2;
		}
		
		return numerator2 / denominator2;
	}
	
	public double getOut3(double Input1, double Input2, double Input3, double Input4, double Input5){
		double h3, y3;
		for(int i = 0; i < noOfRules; i++){
			h3 = Rules.get(i).ruleHeight(Input1, Input2, Input3, Input4, Input5);
			if (h3 > 0){
				y3 = Rules.get(i).getCenter3();
				numerator3 = numerator3 + (h3 * y3);
				denominator3 = denominator3 + h3;
			}
		}
		
		if(denominator3 == 0){
			denominator3 = 1;
			numerator3 = prom3;
		}
		
		return numerator3 / denominator3;
	}
}
